package cs6238.common.message.command;

import static org.junit.Assert.*;

import java.util.UUID;

import org.junit.Test;

import cs6238.common.DelegateEntry;
import cs6238.common.message.MessageFactory;
import cs6238.common.message.command.DelegateCommand;
import cs6238.common.message.command.GetFileCommand;
import cs6238.common.message.command.LsCommand;
import cs6238.common.message.command.PutFileCommand;
import cs6238.test.ByteArrayAssert;

public class CommandSerialization {

	@Test
	public void testLsCommand_CanBeSerializedAndDeserialized() {
		LsCommand command = new LsCommand();
		byte[] buffer = command.serialize();
		
		LsCommand lsCommand = (LsCommand)MessageFactory.createCommand(buffer);
		assertNotNull(lsCommand);
	}
	
	@Test
	public void testGetFileCommand_CanBeSerializedAndDeserialized() {
		GetFileCommand command = new GetFileCommand();
		command.setFileId(UUID.randomUUID());
		byte[] buffer = command.serialize();
		
		GetFileCommand getFileCommand = (GetFileCommand) MessageFactory.createCommand(buffer);	
		assertTrue(getFileCommand.getFileId().equals(command.getFileId()));
	}
	
	
	@Test
	public void testPutFileCommand_CanBeSerializedAndDeserialized() {
		PutFileCommand command = new PutFileCommand();
		command.setClientFilename("clientFilename");
		byte[] buffer = command.serialize();
		
		PutFileCommand putFileCommand = (PutFileCommand) MessageFactory.createCommand(buffer);
		assertEquals(command.getClientFilename(), putFileCommand.getClientFilename());
	}
	
	@Test
	public void testDelegateCommand_CanBeSerializedAndDeserialized() {
		DelegateCommand command = new DelegateCommand();
		DelegateEntry entry = new DelegateEntry(UUID.randomUUID(), "owningHost", "delegatedHost", System.currentTimeMillis() + 1000000, false);
		command.setDelegateEntry(entry);
		
		
		byte[] buffer = command.serialize();
		
		DelegateCommand delegateCommand = (DelegateCommand) MessageFactory.createCommand(buffer);
		DelegateEntry entry2 = delegateCommand.getDelegateEntry();
		
		
		assertEquals(entry.getDelegatedHost(), entry2.getDelegatedHost());
		assertEquals(entry.getExpiration(), entry2.getExpiration());
		assertEquals(entry.getFileId(), entry2.getFileId());
		assertEquals(entry.getOwningHost(), entry2.getOwningHost());
		ByteArrayAssert.Equal(entry.getSignature(), entry2.getSignature());
		assertEquals(entry.getStar(), entry2.getStar());
	}
	
	@Test
	public void testDelegateCommand_WithSignature_CanBeSerializedAndDeserialized() {
		DelegateCommand command = new DelegateCommand();
		DelegateEntry entry = new DelegateEntry(UUID.randomUUID(), "owningHost", "delegatedHost", System.currentTimeMillis() + 1000000, false);
		entry.setSignature(new byte[]{0,1,2,3,4,5,6,7,8,9});
		command.setDelegateEntry(entry);
		
		
		byte[] buffer = command.serialize();
		
		DelegateCommand delegateCommand = (DelegateCommand) MessageFactory.createCommand(buffer);
		DelegateEntry entry2 = delegateCommand.getDelegateEntry();
		
		
		assertEquals(entry.getDelegatedHost(), entry2.getDelegatedHost());
		assertEquals(entry.getExpiration(), entry2.getExpiration());
		assertEquals(entry.getFileId(), entry2.getFileId());
		assertEquals(entry.getOwningHost(), entry2.getOwningHost());
		ByteArrayAssert.Equal(entry.getSignature(), entry2.getSignature());
		assertEquals(entry.getStar(), entry2.getStar());
	}
}
